package com.igo.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igo.domain.datacollection.devicedata.DeviceDataCalcd;
import com.igo.domain.datacollection.result.Result;
import com.igo.domain.datacollection.vo.DeviceDataCalcdVO;
import com.igo.mapper.devicedata.DeviceDataCalcdMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/deviceDataCalcd")
@Api(value = "任务性接口", tags = {"任务性接口"})
public class DeviceDataCalcdController {

    @Resource
    private DeviceDataCalcdMapper deviceDataCalcdMapper;

    @GetMapping("/page")
    @ResponseBody
    @ApiOperation("任务性数据列表")
    public Result page(HttpServletRequest request) {
        String page = request.getParameter("page");
        //当前页数
        String size = request.getParameter("size");
        //页中个数
        //封装的分页对象
        LambdaQueryWrapper<DeviceDataCalcd> deviceDataCalcdLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(page) && StringUtils.isNotBlank(size)) {
            DeviceDataCalcdVO igoCodingVo =new DeviceDataCalcdVO();
            //mybatis-plus中IPage
            IPage<DeviceDataCalcd> list = new Page<>(Long.valueOf(page), Long.valueOf(size));//参数一是当前页，参数二是每页个数

            //按照规则查找并且分页 查找规则可以为空
            if (StringUtils.isNotBlank(request.getParameter("deviceNo"))) {
                deviceDataCalcdLambdaQueryWrapper.eq(DeviceDataCalcd::getDeviceNo, request.getParameter("deviceNo"));
            }
            if (StringUtils.isNotBlank(request.getParameter("startTime"))) {
                deviceDataCalcdLambdaQueryWrapper.ge(DeviceDataCalcd::getStartTime, request.getParameter("startTime"));
            }
            if (StringUtils.isNotBlank(request.getParameter("endTime"))) {
                deviceDataCalcdLambdaQueryWrapper.le(DeviceDataCalcd::getEndTime, request.getParameter("endTime"));
            }
            //任务类型 0:平板车 1:门吊
            if (StringUtils.isNotBlank(request.getParameter("gantryCraneType"))) {
                deviceDataCalcdLambdaQueryWrapper.eq(DeviceDataCalcd::getGantryCraneType, request.getParameter("gantryCraneType"));
            }
            //负载类型 1:大负载 2:小负载
            if (StringUtils.isNotBlank(request.getParameter("loadType"))) {
                deviceDataCalcdLambdaQueryWrapper.eq(DeviceDataCalcd::getLoadType, request.getParameter("loadType"));
            }
            //任务状态 1:有效 2:无效
            if (StringUtils.isNotBlank(request.getParameter("loadValid"))) {
                deviceDataCalcdLambdaQueryWrapper.eq(DeviceDataCalcd::getLoadValid, request.getParameter("loadValid"));
            }
            //计算类型 1:负载 2:空载 3:加油
            if (StringUtils.isNotBlank(request.getParameter("calcdType"))) {
                deviceDataCalcdLambdaQueryWrapper.eq(DeviceDataCalcd::getCalcdType, request.getParameter("calcdType"));
            }
            deviceDataCalcdMapper.selectPage(list, deviceDataCalcdLambdaQueryWrapper.orderByAsc(DeviceDataCalcd::getStartTime));
            list.getRecords().stream().forEach(one->{
                if (one.getLatLongList()!=null){
                    List<String> latLongList = one.getLatLongList().stream().filter(two -> !two.equals("null,null")).collect(Collectors.toList());
                    one.setLatLongList(latLongList);
                }
            });
            //封装
            igoCodingVo.setCurrent(page);
            igoCodingVo.setSize(size);
            igoCodingVo.setTotal(list.getTotal());
            igoCodingVo.setDeviceDataCalcdList(list.getRecords());
            return Result.success(igoCodingVo);
        }else {
            DeviceDataCalcdVO igoCodingVo =new DeviceDataCalcdVO();
            //mybatis-plus中IPage
            //按照规则查找并且分页 查找规则可以为空
            if (StringUtils.isNotBlank(request.getParameter("deviceNo"))) {
                deviceDataCalcdLambdaQueryWrapper.eq(DeviceDataCalcd::getDeviceNo, request.getParameter("deviceNo"));
            }
            if (StringUtils.isNotBlank(request.getParameter("startTime"))) {
                deviceDataCalcdLambdaQueryWrapper.ge(DeviceDataCalcd::getStartTime, request.getParameter("startTime"));
            }
            if (StringUtils.isNotBlank(request.getParameter("endTime"))) {
                deviceDataCalcdLambdaQueryWrapper.le(DeviceDataCalcd::getEndTime, request.getParameter("endTime"));
            }
            //任务类型 0:平板车 1:门吊
            if (StringUtils.isNotBlank(request.getParameter("gantryCraneType"))) {
                deviceDataCalcdLambdaQueryWrapper.eq(DeviceDataCalcd::getGantryCraneType, request.getParameter("gantryCraneType"));
            }
            //负载类型 1:大负载 2:小负载
            if (StringUtils.isNotBlank(request.getParameter("loadType"))) {
                deviceDataCalcdLambdaQueryWrapper.eq(DeviceDataCalcd::getLoadType, request.getParameter("loadType"));
            }
            //任务状态 1:有效 2:无效
            if (StringUtils.isNotBlank(request.getParameter("loadValid"))) {
                deviceDataCalcdLambdaQueryWrapper.eq(DeviceDataCalcd::getLoadValid, request.getParameter("loadValid"));
            }
            //计算类型 1:负载 2:空载 3:加油
            if (StringUtils.isNotBlank(request.getParameter("calcdType"))) {
                deviceDataCalcdLambdaQueryWrapper.eq(DeviceDataCalcd::getCalcdType, request.getParameter("calcdType"));
            }
            igoCodingVo.setDeviceDataCalcdList(deviceDataCalcdMapper.selectList(deviceDataCalcdLambdaQueryWrapper.orderByAsc(DeviceDataCalcd::getStartTime)));
            igoCodingVo.getDeviceDataCalcdList().stream().forEach(one->{
                if (one.getLatLongList()!=null){
                    List<String> latLongList = one.getLatLongList().stream().filter(two -> !two.equals("null,null")).collect(Collectors.toList());
                    one.setLatLongList(latLongList);
                }
            });
            //封装
            return Result.success(igoCodingVo);
        }
    }
}
